﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace TaSki
{
  
    public partial class Form1 : Form
    {
        int aktualny_miech = DateTime.Today.Month-1;
        Rok _2011 = new Rok();
        string b_nazwa, b_data;
        double b_cen;


        private bool IsNumeric(object ValueToCheck)
        {
            double Dummy = 0;
            return double.TryParse(ValueToCheck.ToString(), System.Globalization.NumberStyles.Any, null, out Dummy);
        }

        void wczytajMiesiac(FileStream stream, int nrMiesiaca)
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                while (!reader.EndOfStream)
                {
                    b_nazwa = reader.ReadLine();
                    b_cen = double.Parse(reader.ReadLine());
                    b_data = reader.ReadLine();
                    _2011.rok[nrMiesiaca].Produkty.Add(new Produkt(b_nazwa, b_cen, b_data));

                }
            }
        }

        void wczytajDoProgramu()
        {

            List<FileStream> lista = new List<FileStream>();
            lista.Add(new FileStream(@"styczen.txt", FileMode.OpenOrCreate));
            lista.Add(new FileStream(@"luty.txt", FileMode.OpenOrCreate));
            lista.Add(new FileStream(@"marzec.txt", FileMode.OpenOrCreate));
            lista.Add(new FileStream(@"kwiecien.txt", FileMode.OpenOrCreate));
            lista.Add(new FileStream(@"maj.txt", FileMode.OpenOrCreate));
            lista.Add(new FileStream(@"czerwiec.txt", FileMode.OpenOrCreate));
            lista.Add(new FileStream(@"lipiec.txt", FileMode.OpenOrCreate));
            lista.Add(new FileStream(@"sierpien.txt", FileMode.OpenOrCreate));
            lista.Add(new FileStream(@"wrzesien.txt", FileMode.OpenOrCreate));
            lista.Add(new FileStream(@"pazdziernik.txt", FileMode.OpenOrCreate));
            lista.Add(new FileStream(@"listopad.txt", FileMode.OpenOrCreate));
            lista.Add(new FileStream(@"grudzien.txt", FileMode.OpenOrCreate));
            
            for (var i = 0; i < lista.Count; i++)
            {
                wczytajMiesiac(lista[i],i);
                lista[i].Close();
            }

        }

        void zapiszDoPliku(FileStream stream,int nrMiesiaca)
        {
            using (StreamWriter writer = new StreamWriter(stream))
            {
                for (int i = 0; i < _2011.rok[nrMiesiaca].Produkty.Count(); i++)
                {
                    writer.WriteLine(_2011.rok[nrMiesiaca].Produkty[i].Nazwa_produktu);
                    writer.WriteLine(_2011.rok[nrMiesiaca].Produkty[i].Cena);
                    writer.WriteLine(_2011.rok[nrMiesiaca].Produkty[i].data_wpisu);
                }
            }
        }

        void zapiszZProgramu() 
        {
            List<FileStream> lista = new List<FileStream>();
            lista.Add(new FileStream(@"styczen.txt", FileMode.Open));
            lista.Add(new FileStream(@"luty.txt", FileMode.Open));
            lista.Add(new FileStream(@"marzec.txt", FileMode.Open));
            lista.Add(new FileStream(@"kwiecien.txt", FileMode.Open));
            lista.Add(new FileStream(@"maj.txt", FileMode.Open));
            lista.Add(new FileStream(@"czerwiec.txt", FileMode.Open));
            lista.Add(new FileStream(@"lipiec.txt", FileMode.Open));
            lista.Add(new FileStream(@"sierpien.txt", FileMode.Open));
            lista.Add(new FileStream(@"wrzesien.txt", FileMode.Open));
            lista.Add(new FileStream(@"pazdziernik.txt", FileMode.Open));
            lista.Add(new FileStream(@"listopad.txt", FileMode.Open));
            lista.Add(new FileStream(@"grudzien.txt", FileMode.Open));

            for (var i = 0; i < lista.Count; i++)
            {
                zapiszDoPliku(lista[i], i);
                lista[i].Close();
            }
        }

        public void odswiez() {
            //wyswietlanie (najpierw czyszczenie list z produktami zeby nie dodawac na koncu od nowa
            Lista.Items.Clear();
            for (int i = _2011.rok[_2011.ktory_miech].Produkty.Count()-1; i >=0; i--)
            {
                Lista.Items.Add(_2011.rok[_2011.ktory_miech].Produkty[i].Nazwa_produktu + "\t" +
                _2011.rok[_2011.ktory_miech].Produkty[i].Cena + " zł" + "\t" +
                                _2011.rok[_2011.ktory_miech].Produkty[i].data_wpisu);
            }
            textBox3.Text = _2011.rok[_2011.ktory_miech].Nazwa;
            textBox4.Text = ("" + _2011.rok[_2011.ktory_miech].sum_wyd() + " zł");
            textBox5.Text = ("" + _2011.suma_za_rok() + " zł");
            textBox7.Text = ("" + _2011.rok[_2011.ktory_miech].sum_otrzymanych() + " zł");
            textBox8.Text = ("" + _2011.suma_otrzymanych_za_rok() + " zł");
            textBox6.Text = ("" + _2011.rok[_2011.ktory_miech].srednia_dzienna() );
            //czyszczenie textboxow
            textBox2.Clear();
            textBox1.Clear();
        }
        public Form1()
        {
            InitializeComponent();
            wczytajDoProgramu();
            _2011.ustaw_miech(aktualny_miech);
            if (_2011.ktory_miech == 0) radioButton1.Checked    = true;
            if (_2011.ktory_miech == 1) radioButton2.Checked    = true;
            if (_2011.ktory_miech == 2) radioButton3.Checked    = true;
            if (_2011.ktory_miech == 3) radioButton4.Checked    = true;
            if (_2011.ktory_miech == 4) radioButton5.Checked    = true;
            if (_2011.ktory_miech == 5) radioButton6.Checked    = true;
            if (_2011.ktory_miech == 6) radioButton7.Checked    = true;
            if (_2011.ktory_miech == 7) radioButton8.Checked    = true;
            if (_2011.ktory_miech == 8) radioButton9.Checked    = true;
            if (_2011.ktory_miech == 9) radioButton10.Checked  = true;
            if (_2011.ktory_miech == 10) radioButton11.Checked  = true;
            if (_2011.ktory_miech == 11) radioButton12.Checked  = true;
            
        }

        private void label3_Click(object sender, EventArgs e)
        {
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 
        { 
            Lista.TabStop = false;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            
            //aktualna data
            int dzien = DateTime.Today.Day;
            int miech = DateTime.Today.Month;
            int rok =   DateTime.Today.Year;
            
            //dodawanie produktow do wektora
            if ((textBox2.Text != "") && (textBox1.Text != "") && (IsNumeric(textBox1.Text) == true))
            {
                _2011.rok[_2011.ktory_miech].Produkty.Add(new Produkt(textBox2.Text, double.Parse(textBox1.Text), "\t" +
                    dzien + "." + miech + "." + rok));
                //wyswietlanie z odswiezanien
                odswiez();
            }
            else if ((textBox2.Text == "") || (textBox1.Text == "") || (IsNumeric(textBox1.Text) != true)) 
                MessageBox.Show("Niepoprawnie wprowadzone dane!");
           
        }

        private void textBox2_TextChanged(object sender, EventArgs e) { textBox2.TabIndex = 0; }
        private void textBox1_TextChanged(object sender, EventArgs e) { textBox1.TabIndex = 1; }
        private void groupBox4_Enter(object sender, EventArgs e) { }
        private void textBox3_TextChanged(object sender, EventArgs e) { textBox3.TabIndex = 2; }
 
        private void button3_Click(object sender, EventArgs e)
        {
            if (_2011.rok[_2011.ktory_miech].Produkty.Count() > 0)
            {
                Lista.Items.Clear();
                _2011.rok[_2011.ktory_miech].Produkty.RemoveAt(_2011.rok[_2011.ktory_miech].Produkty.Count - 1);

                for (int i = _2011.rok[_2011.ktory_miech].Produkty.Count()-1;i>=0; i--)
                {
                    Lista.Items.Add(_2011.rok[_2011.ktory_miech].Produkty[i].Nazwa_produktu + "\t" +
                        _2011.rok[_2011.ktory_miech].Produkty[i].Cena + " zł" + "\t" +
                        _2011.rok[_2011.ktory_miech].Produkty[i].data_wpisu);
                }
                textBox3.Text = _2011.rok[_2011.ktory_miech].Nazwa;
                textBox4.Text = ("" + _2011.rok[_2011.ktory_miech].sum_wyd());
                textBox5.Text = ("" + _2011.suma_za_rok() + " zł");
                textBox7.Text = ("" + _2011.rok[_2011.ktory_miech].sum_otrzymanych() + " zł");
                textBox8.Text = ("" + _2011.suma_otrzymanych_za_rok() + " zł");

                string sr = "" + _2011.rok[_2011.ktory_miech].srednia_dzienna() + " zł";
                sr.PadLeft(3, ',');
                textBox6.Text = sr;
            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            zapiszZProgramu();
            Application.Exit();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < Lista.Items.Count; i++)
            {
                if (Lista.SelectedIndex == i)
                {
                    _2011.rok[_2011.ktory_miech].Produkty.RemoveAt(_2011.rok[_2011.ktory_miech].Produkty.Count - i - 1);
                    odswiez();
                }
            }
        }
        private void button5_Click(object sender, EventArgs e)
        {
           var wynik = MessageBox.Show("Czy chcesz usunąć wszystko z danego miesiąca?", "Jesteś pewien?", MessageBoxButtons.YesNo,MessageBoxIcon.Question);
           if(wynik==DialogResult.Yes)
            {
                _2011.rok[_2011.ktory_miech].Produkty.Clear();
                odswiez();
            }
        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        //radio buttony z miesiącami
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true) { _2011.ktory_miech = 0; odswiez(); }
        }
        private void radioButton2_CheckedChanged(object sender, EventArgs e) 
        { 
              if (radioButton2.Checked == true) { _2011.ktory_miech = 1; odswiez(); }
        }
        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton3.Checked == true) { _2011.ktory_miech = 2; odswiez(); }
        }
        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton4.Checked == true) { _2011.ktory_miech = 3; odswiez(); }
        }
        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton5.Checked == true) { _2011.ktory_miech = 4; odswiez(); }
        }
        private void radioButton6_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton6.Checked == true) { _2011.ktory_miech = 5; odswiez(); }
        }
        private void radioButton7_CheckedChanged(object sender, EventArgs e)
        {
           if (radioButton7.Checked == true) { _2011.ktory_miech = 6; odswiez(); }
        }
        private void radioButton8_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton8.Checked == true) { _2011.ktory_miech = 7; odswiez(); }
        }
        private void radioButton9_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton9.Checked == true) { _2011.ktory_miech = 8; odswiez(); }
        }
        private void radioButton10_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton10.Checked == true) { _2011.ktory_miech = 9; odswiez(); }
        }
        private void radioButton11_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton11.Checked == true) { _2011.ktory_miech = 10; odswiez(); }
        }
        private void radioButton12_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton12.Checked == true) { _2011.ktory_miech = 11; odswiez(); }
           
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {       }
        private void pictureBox2_Click(object sender, EventArgs e)
        { textBox2.Text = "Chleb";   }
        private void pictureBox3_Click(object sender, EventArgs e)
        {textBox2.Text = "Makaron";    }
        private void pictureBox4_Click(object sender, EventArgs e)
        { textBox2.Text = "Pasta do zębów";      }
        private void pictureBox5_Click(object sender, EventArgs e)
        {textBox2.Text = "Piwo";        }
        private void pictureBox6_Click(object sender, EventArgs e)
        { textBox2.Text = "Przejazd"; textBox1.Text = "10"; }
        private void pictureBox7_Click(object sender, EventArgs e)
        {textBox2.Text = "Woda";        }
        private void pictureBox8_Click(object sender, EventArgs e)
        {textBox2.Text = "Papier toaletowy";        }
        private void pictureBox9_Click(object sender, EventArgs e)
        { textBox2.Text = "Ser";        }
        private void pictureBox10_Click(object sender, EventArgs e)
        { textBox2.Text = "Hebrata"; }
        private void pictureBox11_Click(object sender, EventArgs e)
        { textBox2.Text = "Kawa";        }
        private void pictureBox20_Click(object sender, EventArgs e)
        { textBox2.Text = "Pomidor"; }
        private void pictureBox19_Click(object sender, EventArgs e)
        { textBox2.Text = "Ryba"; }
        private void pictureBox18_Click(object sender, EventArgs e)
        { textBox2.Text = "Banan"; }
        private void pictureBox14_Click(object sender, EventArgs e)
        { textBox2.Text = "Benzyna"; }
        private void pictureBox16_Click(object sender, EventArgs e)
        { textBox2.Text = "Ciasto"; }
        private void pictureBox15_Click(object sender, EventArgs e)
        { textBox2.Text = "Chipsy"; }
        private void pictureBox25_Click(object sender, EventArgs e)
        { textBox2.Text = "Olej"; }
        private void pictureBox22_Click(object sender, EventArgs e)
        { textBox2.Text = "Ziemniaki"; }
        private void pictureBox12_Click(object sender, EventArgs e)
        { textBox2.Text = "Kino"; }
        private void pictureBox21_Click(object sender, EventArgs e)
        { textBox2.Text = "Gaz"; }
        private void pictureBox17_Click(object sender, EventArgs e)
        {textBox2.Text = "Doładowanie Era";       }
        private void pictureBox24_Click(object sender, EventArgs e)
        { textBox2.Text = "Doładowanie Heyah"; }
        private void pictureBox13_Click(object sender, EventArgs e)
        { textBox2.Text = "Doładowanie Orange"; }
        private void pictureBox23_Click(object sender, EventArgs e)
        { textBox2.Text = "Doładowanie Play"; }
        private void pictureBox26_Click(object sender, EventArgs e)
        { textBox2.Text = "Doładowanie Plus"; }
        private void pictureBox29_Click(object sender, EventArgs e)
        { textBox2.Text = "Doładowanie MobilKing"; }
        private void pictureBox33_Click(object sender, EventArgs e)
        { textBox2.Text = "Doładowanie OneTelefon"; }
        private void pictureBox30_Click(object sender, EventArgs e)
        { textBox2.Text = "Doładowanie Cyfrowy Polsat"; }
        private void pictureBox31_Click(object sender, EventArgs e)
        { textBox2.Text = "Doładowanie TelePin"; }
        private void pictureBox27_Click(object sender, EventArgs e)
        { textBox2.Text = "Doładowanie Sami Swoi"; }
        private void pictureBox32_Click(object sender, EventArgs e)
        { textBox2.Text = "Doładowanie GaduAir"; }
        private void pictureBox28_Click(object sender, EventArgs e)
        { textBox2.Text = "Baterie"; }
        //przyciski - reszta
        private void button7_Click(object sender, EventArgs e)
        {            Application.Exit();        }
        private void button6_Click(object sender, EventArgs e)
        {
            MessageBox.Show("TaSki v1.01 beta - Program zliczający dla studenta.\nZa wszelkie błędy przepraszam :-) ","O programie TaSki",MessageBoxButtons.OK,MessageBoxIcon.Information);
            
        }
        private void textBox5_TextChanged(object sender, EventArgs e)
        {

        }
        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }
        private void textBox7_TextChanged(object sender, EventArgs e)
        {

        }
        private void textBox8_TextChanged(object sender, EventArgs e)
        {

        }
        private void textBox6_TextChanged(object sender, EventArgs e)
        {

        }

        private void label12_Click(object sender, EventArgs e)
        {

        }

        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {

        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
       
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button8_Click(object sender, EventArgs e)
        {
            int druk;
            MessageBox.Show("Drukujemy okienko :-)");
           // printForm1.Print();
            
        }

        private void button9_Click(object sender, EventArgs e)
        {
            zapiszZProgramu();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }
    } 
}
